﻿using S7.Net;
using System;
using System.Windows.Forms;
using S7PlcDataManager.S7Plc;
using ZbSkin.Frm;
using ZbSkin.Tools;

namespace S7PlcDataManager.Controls
{
    public partial class DataQueryControl : UserControl
    {
        private readonly S7PlcHelper _s7PlcHelper;
        private string _variable;
        private VarType _valueType;

        public DataQueryControl(S7PlcHelper s7PlcHelper)
        {
            InitializeComponent();
            _s7PlcHelper = s7PlcHelper;
        }

        private void DataQueryControl_Load(object sender, EventArgs e)
        {
            SetRegion();
        }

        private void DataQueryControl_Resize(object sender, EventArgs e)
        {
            SetRegion();
        }

        private void SetRegion()
        {
            this.SetRegion(30);
        }

        public void ChangeDataType(string variable, VarType valueType)
        {
            _variable = variable;
            _valueType = valueType;
        }

        private void btn_query_Click(object sender, EventArgs e)
        {
            if (!_s7PlcHelper.IsConnected)
            {
                MessageBoxEx.Warning(@"PLC未连接", ParentForm);
                return;
            }

            tb_variable.Text = _variable;
            tb_valtype.Text = _valueType.ToString();

            QueryPlcData();
        }

        private void QueryPlcData()
        {
            switch (_valueType)
            {
                case VarType.String:
                    ReadPlcString();
                    break;
                case VarType.S7String:
                    ReadPlcS7String();
                    break;
                default:
                    ReadPlcObject();
                    break;
            }
        }

        private void ReadPlcObject()
        {
            object value;
            if (_s7PlcHelper.ReadVariable(_variable, out value))
            {
                var val = GetVariableValue(_valueType, value);
                if (val == null)
                {
                    MessageBoxEx.Warning($@"不支持的数据类型[{_valueType}]", ParentForm);
                    tb_val.Text = value?.ToString();
                }
                else
                {
                    tb_val.Text = val.ToString();
                }
            }
            else
            {
                MessageBoxEx.Error(_s7PlcHelper.LastErrorMsg, ParentForm);
            }
        }

        private void ReadPlcString()
        {
            string value;
            if (_s7PlcHelper.ReadString(_variable, out value))
            {
                tb_val.Text = value;
            }
            else
            {
                MessageBoxEx.Error(_s7PlcHelper.LastErrorMsg, ParentForm);
            }
        }

        private void ReadPlcS7String()
        {
            string value;
            if (_s7PlcHelper.ReadS7String(_variable, out value))
            {
                tb_val.Text = value;
            }
            else
            {
                MessageBoxEx.Error(_s7PlcHelper.LastErrorMsg, ParentForm);
            }
        }

        private static object GetVariableValue(VarType varType, object var)
        {
            switch (varType)
            {
                case VarType.Bit:
                    return (bool) var;
                case VarType.Byte:
                    return (byte) var;
                case VarType.Word:
                    return (ushort) var;
                case VarType.DWord:
                    return (uint) var;
                case VarType.Int:
                    return (short) var;
                case VarType.DInt:
                    return (int) var;
                case VarType.Real:
                    return ((uint) var).ConvertToFloat();
                case VarType.LReal:
                    return (double) var;
                default:
                    return null;
            }
        }
    }
}
